﻿using System;
using System.Collections.Generic;
using System.Text;
using Tools.Cache;
using Tools.Utils;

namespace Tools.Service
{
    /// <summary>
    /// 获取文本文件中的知识
    /// </summary>
    public class TxtContentService
    {
        /// <summary>
        /// 情感语录
        /// </summary>
        private const string feelingKey = "feeling";

        /// <summary>
        /// 鸡汤
        /// </summary>
        private const string chickenSoupKey = "chicken_soup";

        /// <summary>
        /// 特殊符号
        /// </summary>
        private const string symbolKey = "symbol";

        /// <summary>
        /// 常用电话
        /// </summary>
        private const string phoneKey = "phone";

        /// <summary>
        /// 国家及首都
        /// </summary>
        private const string countryKey = "country";

        /// <summary>
        /// 国家货币
        /// </summary>
        private const string countryMoneyKey = "country_money";

        /// <summary>
        /// 情感语录
        /// </summary>
        public string GetFeeling()
        {
            var cache = new CacheHelper();
            var list = new List<string>();
            var obj = cache.Get(feelingKey);

            if (obj == null)
            {
                list = FileHelper.FileReadList(feelingKey);
                cache.Set(feelingKey, list, new TimeSpan(1, 0, 0), true);
            }
            else
            {
                list = obj as List<string>;
            }

            var number = new Random().Next(list.Count);

            return list[number];
        }

        /// <summary>
        /// 鸡汤
        /// </summary>
        public string GetChickenSoup()
        {
            var cache = new CacheHelper();
            var list = new List<string>();
            var obj = cache.Get(chickenSoupKey);

            if (obj == null)
            {
                list = FileHelper.FileReadList(chickenSoupKey);
                cache.Set(chickenSoupKey, list, new TimeSpan(1, 0, 0), true);
            }
            else
            {
                list = obj as List<string>;
            }

            var number = new Random().Next(list.Count);

            return list[number];
        }

        /// <summary>
        /// 特殊字符
        /// </summary>
        public List<string> GetSymbol()
        {
            var cache = new CacheHelper();
            var list = new List<string>();
            var obj = cache.Get(symbolKey);

            if (obj == null)
            {
                list = FileHelper.FileReadList(symbolKey);
                cache.Set(symbolKey, list, new TimeSpan(1, 0, 0), true);
            }
            else
            {
                list = obj as List<string>;
            }

            return list.FindAll(t => !string.IsNullOrWhiteSpace(t));
        }

        /// <summary>
        /// 常用电话
        /// </summary>
        public List<string> GetPhone()
        {
            var cache = new CacheHelper();
            var list = new List<string>();
            var obj = cache.Get(phoneKey);

            if (obj == null)
            {
                list = FileHelper.FileReadList(phoneKey);
                cache.Set(phoneKey, list, new TimeSpan(1, 0, 0), true);
            }
            else
            {
                list = obj as List<string>;
            }

            return list.FindAll(t => !string.IsNullOrWhiteSpace(t));
        }

        /// <summary>
        /// 获取世界各国和首都
        /// </summary>
        public List<string> GetCountry()
        {
            var cache = new CacheHelper();
            var list = new List<string>();
            var obj = cache.Get(countryKey);

            if (obj == null)
            {
                list = FileHelper.FileReadList(countryKey);
                cache.Set(countryKey, list, new TimeSpan(1, 0, 0), true);
            }
            else
            {
                list = obj as List<string>;
            }

            return list.FindAll(t => !string.IsNullOrWhiteSpace(t));
        }

        /// <summary>
        /// 国家货币
        /// </summary>
        public List<string> GetCountryMoney()
        {
            var cache = new CacheHelper();
            var list = new List<string>();
            var obj = cache.Get(countryMoneyKey);

            if (obj == null||(obj as List<string>).Count==0)
            {
                list = FileHelper.FileReadList(countryMoneyKey);
                cache.Set(countryMoneyKey, list, new TimeSpan(1, 0, 0), true);
            }
            else
            {
                list = obj as List<string>;
            }

            return list.FindAll(t => !string.IsNullOrWhiteSpace(t));
        }
    }
}
